﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kelper.Domain.Models;
using Kelper.Domain.Data.Abstract;
using Kelper.ViewModels;

namespace Kelper.Controllers
{
    public class CategoryController : BaseController
    {
        private ICategoryRepository categories;

        public CategoryController(ICategoryRepository repo)
        {
            categories = repo;
        }

        public ViewResult Index()
        {
            return View(categories.Categories.OrderBy(c => c.Name));
        }

        public new ActionResult View(string name, int page = 1)
        {
            Category category = categories.Categories.FirstOrDefault(c => c.Name == name);
            if (category == null)
                return HttpNotFound();

            IEnumerable<Recipe> recipes = category.Recipes.OrderByDescending(r => r.Created);
            IEnumerable<Recipe> items = Paginate(recipes, page);

            ViewCategoryViewModel viewModels = new ViewCategoryViewModel    // Extend and enable paging
            {
                Name = category.Name,
                Recipes = items,
                CurrentPage = page,
                ItemsPerPage = PageSize,
                TotalItems = recipes.Count()
            };

            return View(viewModels);
        }
    }
}
